//
//  DiscoverViewController.m
//  pocketNews
//
//  Created by DS on 2018/10/26.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "DiscoverViewController.h"

//点击美女
#import "BeutyGirlsViewController.h"


//计算器
#import "CalculatorViewController.h"

#import "OperationViewController.h"


//日历
#import "CalendarViewController.h"


//扫码工具
#import "QcodeViewController.h"


//天气
#import "WeatherViewController.h"

#import "HomeSonModel.h"

#import <CoreLocation/CoreLocation.h>


//发现发布
#import "PublicViewController.h"



#import "BtnViewController.h"





@interface DiscoverViewController ()<CLLocationManagerDelegate>

Strong UIView * BlueView;

//未开发模块

Strong UIView * firstView;
Strong UILabel * firstlabel;

//美女

Strong UIView * girlView;
Strong UILabel * girlLabel;
Strong UIImageView * girlImageView;


//天气

Strong UIView * weatherView;
Strong UILabel * weatherLabel;
Strong UIImageView * weatherImageView;



//日历
Strong UIView * calView;
Strong UILabel * calLabel;
Strong UIImageView * calImageview;



//二维码生成器

Strong UIView * codeView;
Strong UIView * codeLabel;
Strong UIImageView * codeImageView;

//计算器

Strong UIView * jisuanView;
Strong UILabel * jisunLabel;
Strong UIImageView * jisuanImageView;

Strong NSMutableArray * dataArr;


Strong NSString * cityName;

//定义定位属性

@property(nonatomic,strong) CLLocationManager *locationManager;

@end

//网络
@implementation DiscoverViewController(NetWorking)
//新闻接口
- (void)getData{
    
    
    
    [self showprogressHUD];
    NSDictionary *parameters = @{
                                 
                                 @"type":@"头条",
                                 
                                 @"key":@"c6e4817b12d373379556ffb70a5af0cb"
                                 
                                 }.mutableCopy;
    
    [[AFAppDotNetAPIClient sharedClient] POST:@"http://v.juhe.cn/toutiao/index" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        
        
        if ([responseObject[@"result"][@"stat"] intValue] == 1) {
            
            for (NSDictionary * dic in responseObject[@"result"][@"data"]) {
                
                HomeSonModel * model = [[HomeSonModel alloc] init];
                
                [model HomeSonModelWithDic:dic];
                
                [self.dataArr addObject:model];
                
            }
          
        }else{
            
            ShowMessage(responseObject[@"reason"]);
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
        
        NSLog(@"%@",error);
    }];
}
@end

@implementation DiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.BlueView];
    [self.view addSubview:self.firstView];
    [self.view addSubview:self.girlView];
    [self.view addSubview:self.weatherView];
    [self.view addSubview:self.calView];
    [self.view addSubview:self.codeView];
    [self.view addSubview:self.jisuanView];
    
    [self getData];
    
    [self startLocation];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;

    
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
     self.navigationController.navigationBar.hidden = NO;
}


#pragma mark ---- Action

- (void)discoverClick{
    
    BtnViewController * vc = [[BtnViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}

 //点击美女

- (void)tapGirlClick{
 
    BeutyGirlsViewController * vc = [[BeutyGirlsViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];

}

//日历点击

- (void)calTapClick{
    
    CalendarViewController * vc = [[CalendarViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.dataArr = self.dataArr;
    [self.navigationController pushViewController:vc animated:YES];
    
    
}

//计算器点击

static int i = 0;

-(void)JisuanTapClick{
    
    //两款计算器的显示
    
    i ++;
    
    if (i % 2 == 0) {
        
        OperationViewController * vc = [[OperationViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
        
    }else{
        
         CalculatorViewController * vc = [[CalculatorViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


//扫码工具
- (void)QcodecodTapClick{
    
    QcodeViewController * vc = [[QcodeViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
    
}

#pragma mark - 获取当前定位城市

-(void)startLocation{
    
    if ([CLLocationManager locationServicesEnabled]) {//判断定位操作是否被允许
        
        self.locationManager = [[CLLocationManager alloc] init];
        
        self.locationManager.delegate = self;//遵循代理
        
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        
        self.locationManager.distanceFilter = 10.0f;
        
        [_locationManager requestWhenInUseAuthorization];//使用程序其间允许访问位置数据（iOS8以上版本定位需要）
        
        [self.locationManager startUpdatingLocation];//开始定位
        
    }else{//不能定位用户的位置的情况再次进行判断，并给与用户提示
        
        //1.提醒用户检查当前的网络状况
        
        //2.提醒用户打开定位开关
        
    }
    
}

//实现定位协议回调方法

#pragma mark - CoreLocation Delegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    
    //当前所在城市的坐标值
    CLLocation *currLocation = [locations lastObject];
    
    NSLog(@"经度=%f 纬度=%f 高度=%f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude);
    
    //根据经纬度反向地理编译出地址信息
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    
    [geoCoder reverseGeocodeLocation:currLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        
        for (CLPlacemark * placemark in placemarks) {
            
            NSDictionary *address = [placemark addressDictionary];
            
            //  Country(国家)  State(省)  City（市）
            NSLog(@"#####%@",address);
            
            NSLog(@"%@", [address objectForKey:@"Country"]);
            
            NSLog(@"%@", [address objectForKey:@"State"]);
            
            NSLog(@"%@", [address objectForKey:@"City"]);
            
            self.cityName = [address objectForKey:@"City"];
            
            
           
     
            
        }
        
    }];
    
}


-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    
    if ([error code] == kCLErrorDenied){
        //访问被拒绝
    }
    if ([error code] == kCLErrorLocationUnknown) {
        //无法获取位置信息
    }
}



//天气点击

- (void)weatherTapClick{
   
    WeatherViewController * vc = [[WeatherViewController alloc] init];
    
    vc.hidesBottomBarWhenPushed = YES;
    
    vc.cityName = self.cityName;
    
    [self.navigationController pushViewController:vc animated:YES];
    
    
}

#pragma mark --- Init

- (UIView *)BlueView{
    
    if (!_BlueView ) {
        
        _BlueView = [[UIView alloc] init];
        _BlueView.frame = CGRectMake(0, 0, SCREEN_WIDTH, UNIT_HEIGHT(30));
        _BlueView.backgroundColor = getColor(@"1E7CD2");
        
    }
    
    return _BlueView;
    
}

- (UIView *)firstView{
    
    if (!_firstView) {
        
        _firstView = [UIView addViewWithFrame:CGRectMake(UNIT_WIDTH(10), UNIT_HEIGHT(40), UNIT_WIDTH(250), UNIT_HEIGHT(150)) WithBackgroundColor:getColor(@"FFA752")];
        
        _firstView.layer.cornerRadius = 5;
        _firstlabel.layer.masksToBounds = YES;
        
        _firstlabel = [UILabel addLabelWithFrame:CGRectMake(0, 0, 0, 0) AndText:@"发现发布" AndFont:20 AndAlpha:1.0 AndColor:getColor(whiteColor)];
        _firstlabel.textAlignment = NSTextAlignmentCenter;
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(discoverClick)];
        
        [_firstView addGestureRecognizer:tap];
        
        
        [_firstView addSubview:self.firstlabel];
        
        [self.firstlabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.firstView.mas_centerX);
            make.centerY.mas_equalTo(self.firstView.mas_centerY);
            make.height.mas_equalTo(UNIT_HEIGHT(30));
            make.width.mas_equalTo(self.firstView.width);
        }];
        
    }
    
    return _firstView;
    
}

//美女

-(UIView *)girlView{
    
    if (!_girlView) {
        
        _girlView = [UIView addViewWithFrame:CGRectMake(UNIT_WIDTH(20)+ UNIT_WIDTH(250), UNIT_HEIGHT(40), SCREEN_WIDTH - UNIT_WIDTH(30) - UNIT_WIDTH(250), UNIT_HEIGHT(150)) WithBackgroundColor:getColor(navColor)];
        _girlView.layer.cornerRadius = 5;
        _girlView.layer.masksToBounds = YES;
        _girlView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGirlClick)];
        
        [_girlView addGestureRecognizer:tap];
        
        _girlLabel = [UILabel addLabelWithFrame:CGRectMake(0, 0, 0, 0) AndText:@"图片展示" AndFont:20 AndAlpha:1.0 AndColor:getColor(whiteColor)];
        _girlLabel.textAlignment = NSTextAlignmentCenter;
        
        _girlImageView = [UIImageView addImgWithFrame:CGRectMake(0, 0, 0, 0) AndImage:@"icon02"];
        
        [_girlView sd_addSubviews:@[self.girlLabel,self.girlImageView]];
        
        [self.girlLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(UNIT_HEIGHT(20));
            make.centerX.mas_equalTo(self.girlView.mas_centerX);
            make.height.mas_equalTo(UNIT_HEIGHT(30));
        }];
        
        [self.girlImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(UNIT_WIDTH(-20));
            make.bottom.mas_equalTo(UNIT_HEIGHT(-30));
            make.height.mas_equalTo(UNIT_HEIGHT(44));
            make.width.mas_equalTo(UNIT_WIDTH(44));
            
        }];
        
        
        
    }
    
    return _girlView;
    
}

- (UIView *)weatherView{
    
    if (!_weatherView) {
        
        _weatherView = [UIView addViewWithFrame:CGRectMake(UNIT_WIDTH(10), UNIT_HEIGHT(30) + UNIT_HEIGHT(10) + UNIT_HEIGHT(160), SCREEN_WIDTH - UNIT_WIDTH(10) * 2, UNIT_HEIGHT(140)) WithBackgroundColor:getColor(@"0079E0")];
        _weatherView.layer.cornerRadius = 5;
        _weatherView.layer.masksToBounds = YES;
        
        _weatherView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(weatherTapClick)];
        
        [_weatherView addGestureRecognizer:tap];
        
        _weatherLabel = [UILabel addLabelWithFrame:CGRectMake(0, 0, 0, 0) AndText:@"天气" AndFont:20 AndAlpha:1.0 AndColor:getColor(whiteColor)];
        
        _weatherImageView = [UIImageView addImgWithImage:@"icon04"];
        
        [self.weatherView sd_addSubviews:@[self.weatherLabel,self.weatherImageView]];
        
        [self.weatherLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(UNIT_WIDTH(150));
            make.centerY.mas_equalTo(self.weatherView.mas_centerY);
            make.height.mas_equalTo(UNIT_HEIGHT(30));
        }];
        
        [self.weatherImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(UNIT_HEIGHT(50));
            make.left.mas_equalTo(self.weatherLabel.mas_right).offset(UNIT_WIDTH(20));
            make.height.mas_equalTo(UNIT_HEIGHT(44));
            make.width.mas_equalTo(UNIT_WIDTH(44));
        }];
        
        
    }
    
    return _weatherView;
    
}

- (UIView *)calView{
    
    if (!_calView) {
        
        _calView = [UIView addViewWithFrame:CGRectMake(UNIT_WIDTH(10), UNIT_HEIGHT(30) + UNIT_HEIGHT(10) + UNIT_HEIGHT(160) + UNIT_HEIGHT(150), (SCREEN_WIDTH - UNIT_WIDTH(10) * 3) /2, UNIT_HEIGHT(140)) WithBackgroundColor:getColor(@"FA5AB1")];
        
        _calView.layer.masksToBounds = YES;
        _calView.layer.cornerRadius = 5;
        
        _calView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(calTapClick)];
        
        [_calView addGestureRecognizer:tap];
        
        
        _calLabel = [UILabel addLabelWithFrame:CGRectMake(0, 0, 0, 0) AndText:@"日历" AndFont:20 AndAlpha:1.0 AndColor:getColor(whiteColor)];
        
        _calImageview = [UIImageView addImgWithImage:@"icon03"];
        
        [self.calView sd_addSubviews:@[self.calLabel,self.calImageview]];
        
        [self.calLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(UNIT_WIDTH(30));
            make.centerY.mas_equalTo(self.calView.mas_centerY);
            make.height.mas_equalTo(UNIT_HEIGHT(30));
        }];
        
        [self.calImageview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.calLabel.mas_right).offset(UNIT_WIDTH(20));
            make.bottom.mas_equalTo(self.calView.mas_bottom).offset(UNIT_HEIGHT(-20));
            make.height.mas_equalTo(UNIT_HEIGHT(44));
            make.width.mas_equalTo(UNIT_WIDTH(44));
        }];
        
        
        
    }
    
    return _calView;
    
}

- (UIView *)codeView{
    
    if (!_codeView) {
        _codeView = [UIView addViewWithFrame:CGRectMake(UNIT_WIDTH(10),UNIT_HEIGHT(30) + UNIT_HEIGHT(10) + UNIT_HEIGHT(160) + UNIT_HEIGHT(150) + UNIT_HEIGHT(150) , (SCREEN_WIDTH - UNIT_WIDTH(10) * 3) /2, UNIT_HEIGHT(140)) WithBackgroundColor:getColor(@"31D267")];
        _codeView.layer.cornerRadius = 5;
        _codeView.layer.masksToBounds = YES;
        
        _codeView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(QcodecodTapClick)];
        
        [_codeView addGestureRecognizer:tap];
        
        _codeLabel = [UILabel addLabelWithFrame:CGRectMake(0, 0, 0, 0) AndText:@"二维码" AndFont:20 AndAlpha:1.0 AndColor:getColor(whiteColor)];
        
        _codeImageView = [UIImageView addImgWithImage:@"5"];
        
        [self.codeView sd_addSubviews:@[self.codeLabel,self.codeImageView]];
        
        [self.codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(UNIT_WIDTH(30));
            make.centerY.mas_equalTo(self.codeView.mas_centerY);
            make.height.mas_equalTo(UNIT_HEIGHT(30));
        }];
        
        [self.codeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.codeLabel.mas_right).offset(UNIT_WIDTH(20));
            make.bottom.mas_equalTo(self.codeView.mas_bottom).offset(UNIT_HEIGHT(-20));
            make.height.mas_equalTo(UNIT_HEIGHT(44));
            make.width.mas_equalTo(UNIT_WIDTH(44));
        }];
        
        
    }
    
    return _codeView;
    
}

- (UIView *)jisuanView{
    
    if (!_jisuanView) {
        
        _jisuanView = [UIView addViewWithFrame:CGRectMake(UNIT_WIDTH(20) + (SCREEN_WIDTH - UNIT_WIDTH(10) * 3) /2,  UNIT_HEIGHT(30) + UNIT_HEIGHT(10) + UNIT_HEIGHT(160) + UNIT_HEIGHT(150), (SCREEN_WIDTH - UNIT_WIDTH(10) * 3) /2, UNIT_HEIGHT(140) * 2 + UNIT_HEIGHT(10)) WithBackgroundColor:getColor(@"FFBF16")];
        _jisuanView.layer.cornerRadius = 5;
        _jisuanView.layer.masksToBounds = YES;
        
        _jisuanView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(JisuanTapClick)];
        
        [_jisuanView addGestureRecognizer:tap];
        
        _jisunLabel = [UILabel addLabelWithFrame:CGRectMake(0, 0, 0, 0) AndText:@"计算器" AndFont:20 AndAlpha:1.0 AndColor:getColor(whiteColor)];
        
        _jisuanImageView = [UIImageView addImgWithImage:@"icon07"];
        
        [self.jisuanView sd_addSubviews:@[self.jisunLabel,self.jisuanImageView]];
        
        [self.jisunLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.jisuanView.mas_top).offset(UNIT_HEIGHT(50));
            make.left.mas_equalTo(self.jisuanView.mas_left).offset(UNIT_WIDTH(30));
            make.height.mas_equalTo(UNIT_HEIGHT(30));
        }];
        
        [self.jisuanImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.mas_equalTo(UNIT_WIDTH(-30));
            make.bottom.mas_equalTo(UNIT_HEIGHT(-50));
            make.height.mas_equalTo(UNIT_HEIGHT(44));
            make.width.mas_equalTo(UNIT_WIDTH(44));
            
        }];
        
    }
    
    return _jisuanView;
    
}


- (NSMutableArray *)dataArr{
    
    if (!_dataArr) {
        
        _dataArr = @[].mutableCopy;
    }
    
    return _dataArr;
    
    
}



@end
